package com.ztools.pc.service.impl.information;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ztools.common.model.pc.export.PointExport;
import com.ztools.common.model.pc.information.InformationPoint;
import com.ztools.common.model.pc.information.WeChatMember;
import com.ztools.common.util.common.PageUtil;
import com.ztools.common.util.common.enumUnit.PointOperationEnum;
import com.ztools.common.util.pc.DictUtil;
import com.ztools.pc.mapper.information.InformationPointMapper;
import com.ztools.pc.service.information.InformationPointService;
import com.ztools.pc.service.information.MemberService;
import com.ztools.pc.service.sys.DictService;
import com.ztools.pc.util.ExcelUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author seeTools
 * @since 2023-06-02
 */
@Service
public class InformationPointServiceImpl extends ServiceImpl<InformationPointMapper, InformationPoint> implements InformationPointService {

    @Resource
    private DictService dictService;
    @Resource
    private MemberService memberService;
    @Resource
    private ExcelUtil excelUtil;

    /**
     * 分页查询
     *
     * @param pageUtil 分页数据
     * @return 对象
     */
    @Override
    public Page<InformationPoint> findList(PageUtil<InformationPoint> pageUtil) {
        List<OrderItem> orders = new ArrayList<>();
        QueryWrapper<InformationPoint> queryWrapper = getQueryWrapper(pageUtil.getParams());
        Page<InformationPoint> pages = new Page<>(pageUtil.getPage(), pageUtil.getSize());
        // 排序数据  StrUtil.toUnderlineCase huTools将驼峰装换为下划线格式
        orders.add(new OrderItem(StrUtil.toUnderlineCase(pageUtil.getSortName()), "asc".equals(pageUtil.getSortOrder())));
        pages.setOrders(orders);
        Page<InformationPoint> page = page(pages, queryWrapper);
        dictionary(page.getRecords());
        return page;
    }

    @Override
    public List<InformationPoint> findByObject(InformationPoint informationPoint) {
        QueryWrapper<InformationPoint> queryWrapper = new QueryWrapper<>(informationPoint);
        return list(queryWrapper);
    }

    @Override
    public boolean update(InformationPoint informationPoint) {
        boolean b = false;
        // 修改前先获取，在删除（乐观锁）
        InformationPoint byId = getById(informationPoint);
        b = updateById(informationPoint);
        return b;
    }

    @Override
    public boolean delete(InformationPoint informationPoint) {
        boolean b = false;
        InformationPoint byId = getById(informationPoint);
        b = removeById(informationPoint);
        return b;
    }


    @Override
    public boolean increase(InformationPoint informationPoint) {
        WeChatMember member = memberService.getById(informationPoint.getMemberId());
        // 增加会员积分
        int point = member.getTotalPoints() + informationPoint.getOperationPoints();
        informationPoint.setOperationType(PointOperationEnum.increase.getOperationType());
        informationPoint.setOperationDate(new Date());
        informationPoint.setBeforePoint(member.getTotalPoints());
        informationPoint.setAfterPoint(point);
        save(informationPoint);
        // 更新会员积分
        member.setTotalPoints(point);
        return memberService.update(member);
    }


    @Override
    public boolean reduce(InformationPoint informationPoint) {
        WeChatMember member = memberService.getById(informationPoint.getMemberId());
        // 减少会员积分
        int point = member.getTotalPoints() - informationPoint.getOperationPoints();
        informationPoint.setOperationType(PointOperationEnum.reduce.getOperationType());
        informationPoint.setOperationDate(new Date());
        informationPoint.setBeforePoint(member.getTotalPoints());
        informationPoint.setAfterPoint(point);
        save(informationPoint);

        member.setTotalPoints(point);
        return memberService.update(member);
    }


    @Override
    public void exportExcel(HttpServletResponse response, InformationPoint informationPoint) {
        long count = count();
        long sheetNumber = ExcelUtil.SHEET_NUMBER;
        long pageSize = excelUtil.getPageSize(sheetNumber, count);
        QueryWrapper<InformationPoint> queryWrapper = getQueryWrapper(informationPoint);
        for (int i = 0; i < pageSize; i++) {
            Page<InformationPoint> pages = new Page<>(i, sheetNumber);
            Page<InformationPoint> page = page(pages, queryWrapper);
            List<InformationPoint> list = page.getRecords();
            dictionary(list);
            excelUtil.exportExcel(response, i, list, PointExport.class, "公众号粉丝基本信息");
        }
    }

    public QueryWrapper<InformationPoint> getQueryWrapper(InformationPoint informationPoint) {
        QueryWrapper<InformationPoint> queryWrapper = new QueryWrapper<>();
        if (informationPoint.getMemberId() != null) {
            queryWrapper.lambda().eq(InformationPoint::getMemberId, informationPoint.getMemberId());
        }
        if (StrUtil.isNotEmpty(informationPoint.getChannel())) {
            queryWrapper.lambda().eq(InformationPoint::getChannel, informationPoint.getChannel());
        }
        if (StrUtil.isNotEmpty(informationPoint.getOperationType())) {
            queryWrapper.lambda().eq(InformationPoint::getOperationType, informationPoint.getOperationType());
        }
        return queryWrapper;
    }

    /**
     * 字典翻译
     *
     * @param informationPointList 对象集合
     */
    public void dictionary(List<InformationPoint> informationPointList) {
        for (InformationPoint informationPoint : informationPointList) {
            dictionary(informationPoint);
        }
    }

    public void dictionary(InformationPoint informationPoint) {
        if (informationPoint != null) {
            if (StrUtil.isNotEmpty(informationPoint.getChannel())) {
                informationPoint.setChannelName(dictService.findByTypeCode(DictUtil.POINT_CHANNEL, informationPoint.getChannel()));
            }
            if (informationPoint.getMemberId() != null) {
                WeChatMember member = memberService.getById(informationPoint.getMemberId());
                informationPoint.setMemberName(member.getName());
            }
            if (StrUtil.isNotEmpty(informationPoint.getOperationType())) {
                informationPoint.setOperationTypeName(dictService.findByTypeCode(DictUtil.OPERATION_TYPE, informationPoint.getOperationType()));
            }
        }
    }

}

